package com.fbty.mt.action.admin;

import com.fbty.base.action.BaseAction;
import com.fbty.base.domain.Page;
import com.fbty.mt.domain.User;
import com.fbty.mt.service.UserService;
import com.fbty.mt.utils.DegistUtil;

@SuppressWarnings("serial")
public class UserManagerAction extends BaseAction {
	
	public UserService userService;
	
	private Page page;
	
	private int pageNo=1;
	
	private int pageSize=20;
	
	private String username="";
	
	private User user;
	
	private int uid;
	private String message;
	private String email;
	
	private String qq;
	
	public String show(){
		
		String tusername= "%"+username+"%";
		page = userService.pageUser(pageNo, pageSize, tusername);
		
		return "show";
	}
	public String addAdmin() throws Exception {
		message = "";
		user.setState(2);
		user.setType(0);
		user.setPassword(DegistUtil.md5Digest(user.getPassword()));
		int state = userService.saveUser(user);
		String tusername= "%"+username+"%";
		page = userService.pageUser(pageNo, pageSize, tusername);
		if(state==-1) {
			message = "该邮箱已被注册，请换一个邮箱!";
		} if(state==-2) {
			message = "该用户名已经存在!";
		} 
		return "show";
	}
	
	public String load(){
		user = userService.findUserById(uid);
		return "load";
	}
	
	public String update(){
		User u = userService.findUserById(uid);
		u.setEmail(email);
		u.setQq(qq);
		userService.updateUser(u);
		return "update";
	}
	public String delAdmin() {
		if(user.getId() != null) {
			userService.deleteUser(user.getId());
			return SUCCESS;
		}
		return "error";
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	public UserService getUserService() {
		return userService;
	}
	public int getPageNo() {
		return pageNo;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public int getPageSize() {
		return pageSize;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}


	public void setUid(int uid) {
		this.uid = uid;
	}


	public void setEmail(String email) {
		this.email = email;
	}


	public void setQq(String qq) {
		this.qq = qq;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	
	
}
